package io.babystar.modules.app.controller;


import io.babystar.common.annotation.SysLog;
import io.babystar.common.utils.DateUtils;
import io.babystar.common.utils.R;
import io.babystar.modules.business.entity.*;
import io.babystar.modules.business.service.*;
import io.babystar.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 个人中心
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/app")
@Api("教练端")
public class AppStudentController {

    @Autowired
    private ParentService parentService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private BaseMemberCardService basememberCardService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private CourseDateService courseDateService;
    @Autowired
    private StudentCourseDateService studentCourseDateService;


    /**
     * 保存
     */
    @SysLog("保存学生")
    @Transactional
    @RequestMapping("/saveStudent")
    public R save(@RequestBody StudentEntityAll student) {
        SysUserEntity user = new SysUserEntity();
        user.setBusinessId(student.getBusinessId());
        user.setUserId(student.getUserId());
        try {
            student.setStudentage(student.getStudentage());
            Long studentId = student.getId();
            if(studentId!=null){
                studentService.updateStudent(student);
            }else{
                studentService.saveStudent(student, user);
            }

        } catch (Exception e) {
            e.printStackTrace();
            R.error();
        }
        return R.ok();
    }

    /**
     * 查询可选所有课程
     *
     * @return
     */
    @RequestMapping("/getCourseApp")
    public R getCourse(String shopid) {
        Map<String, Object> map = new HashMap<>();
        map.put("shop_id", shopid);
        List<CourseEntity> courseEntityList = courseService.listByMap(map);
        return R.ok().put("courseEntityList", courseEntityList);
    }

    /**
     * 查询会员卡状态
     */
    @RequestMapping("/getMemberCardApp")
    public R getMemberCard(String bid){
        List<BaseMemberCardEntity> list = basememberCardService.getMemberCard(Integer.parseInt(bid));
        return R.ok().put("cardList", list);
    }

    /**
     * 查询学员详细信息
     */
    @RequestMapping("/getStudentInfo")
    public R getStudentInfo(String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StudentEntity student = studentService.getById(id);
        ParentEntity parent = parentService.getById(student.getParentId());
        StudentEntityAll studentEntityAll = new StudentEntityAll();
        studentEntityAll.setId(Long.parseLong(student.getId()+""));
        studentEntityAll.setStudentname(student.getStudentName());
        studentEntityAll.setStudentage(sdf.format(student.getStudentAge()));
        studentEntityAll.setRefereename(parent.getRefereeName());
        studentEntityAll.setStudentmather(parent.getParentName());
        studentEntityAll.setMatherphone(parent.getParentPhone());
        studentEntityAll.setOther(parent.getOther());
        studentEntityAll.setOtherphone(parent.getOtherPhone());
        studentEntityAll.setSchool(student.getSchool());
        studentEntityAll.setAddress(student.getAddress());
        studentEntityAll.setCardtype(student.getCardTypeId());
        studentEntityAll.setGiftequipment(student.getEquipment());
        studentEntityAll.setDuetime(student.getDueTime());
        studentEntityAll.setDuetimeStr(sdf.format(student.getDueTime()));
        studentEntityAll.setPayment(student.getPayment());
        studentEntityAll.setResidualclasshour(student.getResidualClassHour());
        studentEntityAll.setClassPeriod(student.getClassPeriod());
        studentEntityAll.setShopid(student.getShopId());
        studentEntityAll.setRemark(student.getRemark());
        studentEntityAll.setConsultant(student.getConsultant());
        studentEntityAll.setWeekLimit(student.getWeekLimit());
        studentEntityAll.setCreatetime(student.getCreateTime());
        return R.ok().put("student", studentEntityAll);
    }


}
